<template>
	<view class='container'>
	  <view class='login_box'>
	    <image src='https://gy-files.oss-cn-shenzhen.aliyuncs.com/work/allAvatar.jpg'></image>
	    <text class='font-15 mgt-5'>同城共享装修</text>
	  </view>
	  <view class='center_box'>
	    <view class='box_one'>
	      <view>
	        <text class='font-15'>请允许同城共享装修获取以下权限</text>
	      </view>
	      <view>
	        <text class='font-13 font-normal'>以下信息仅用于您登录同城共享装修，我们将严格不外泄，更不会对您发送垃圾信息造成骚扰</text>
	      </view>
	    </view>
	    <view class='box_two mgt-20'>
	      <view v-if="is_show">
	        <text class='font-15 login_active'>获取您的公开信息（昵称，头像等）  </text>
	      </view>
	      <view v-else>
	        <text class='font-15 '> 获取您的公开信息（昵称，头像等）</text>
	      </view>
	      <view>
	        <text class='font-13 font-normal'>获取您的公开信息方便您使用全部功能</text>
	      </view>
	    </view>
	    <view class='box_three mgt-20'>
	      <view v-if="is_show">
	        <text class='font-15'>授权手机号成功  </text>
	      </view>
	      <view v-else>
	        <text class='font-15 login_active'>授权手机号成功  </text>
	      </view>
	      <view>
	        <text class='font-13 font-normal'>获取您的手机号用于实名认证</text>
	      </view>
	    </view>
	  </view>
	  <view class='footer_btn'>
	    <button id='1' class='submit-btn submit-margin-top font-18 botCenterButton' hover-class v-if="is_show" open-type='getUserInfo' @getuserinfo='bindGetUserInfo'>第一步 ，授权公开信息</button>
	    <button id='1' class='submit-btn submit-margin-top font-18 botCenterButton' hover-class v-else open-type='getPhoneNumber' @getphonenumber='getPhoneNumber'>第二步，授权手机号</button>
	  </view>
	</view>
</template>

<script>
	import app from '../../App.vue';
	import {
		GYZJMODEL as gyzjModel
	} from '../../utils/gyzjModel.js';
	export default {
		data() {
			return {
				is_show:true,
				userInfo: '',
			}
		},
		created(options){
			
		},
		onLoad(options){
			//回调用户信息
		},
		methods:{
			bindGetUserInfo:function(e){
				console.log('第一次点击的值a',e);
				 if (e.detail.errMsg.indexOf("getUserInfo:fail") >= 0) {
				  uni.hideLoading();
				  return;
				};
				let token = app.globalData.token;
				let _this=this;
				uni.getUserInfo({
				  success: function (res) {
					wx.hideLoading();
					let data = {
					  encryptedData: res.encryptedData,
					  iv: res.iv,
					}
					console.log('点击获取用户信息',res);
					//这里应该是得到用户的信息，但这里没用
					// model.userGetUnion(data, app.globalData.token)
					//真实姓名默认为用户昵称
					e.detail.userInfo.name=e.detail.userInfo.nickName;
					//这里可能需要更新这个用户的信息
					 _this.updateUserInfoFunc(e.detail.userInfo, token);
				  }
				})
			},
			//更新用户信息
			updateUserInfoFunc:function(postData,token){
				uni.showLoading({
				  title: '加载中...',
				})
				gyzjModel.prototype.updataUserInfo(postData, token).then(data => {
					switch (data.code) {
							case 1:
								uni.hideLoading();
								//重新获取用户信息
								this.getUserInfo(token);
								this.is_show=false;
								break;
							default:
								uni.showToast({
									title: data.message,
									icon: 'none'
								});
								break;
						}
				});
			},
			// 获取微信个人信息
			getUserInfo: function (token) {
			    gyzjModel.prototype.getUserInfo(token).then(data => {
				  /*用户第一次提交信息更新数据*/
			      app.globalData.userInfo = data.data.user;
			      uni.setStorageSync("userInfo", data.data.user);
			    })
			  },
			 /*发起手机授权*/
			getPhoneNumber:function(e){
				 let _this=this;
				 if(e.detail.errMsg=='getPhoneNumber:ok'){
					 _this.decodePhone(e);
				 }else{
					 this.closePhone();
				 }
			},
			/*获取手机号码*/
			decodePhone(e){
				let _this=this;
				let token=uni.getStorageSync('token');
				let postData={
					iv:e.detail.iv,
					encryptedData:e.detail.encryptedData,
					sessionKey:decodeURIComponent(token)
				}
				gyzjModel.prototype.decryptPhone(postData).then(data=>{
					uni.showLoading();
					switch(data.code){
						case 1:_this.updataUserInfoPhone(token,{mobile:data.data.phoneNumber})
						break;
						default:
						uni.hideLoading();
						uni.showToast({
							title:data.message,
							icon:'none'
						})
						break;
					}
				})
			},
			/*更新用户手机号码*/
			updataUserInfoPhone:function(token,postData){
				let _this=this;
				gyzjModel.prototype.updataUserInfo(postData,token).then(data=>{
					switch(data.code){
						case 1:
						uni.hideLoading();
						_this.getUserInfo(token);
						_this.closePhone();
						break;
						default:
						uni.showModal({
							content:data.message,
							showCancel:false,
							confirmColor:'#f94c48'
						})
						break;
					}
					uni.hideLoading();
				})
			},
			/*手机不授权跳转*/
			closePhone:function(){
				uni.navigateTo({
					url:'/pages/index/index'
				})
			}
		}
	}
</script>

<style>
	@import "../../static/style/weui.css";
	@import "../../static/style/public.css";
	.container{
	  padding:70upx 30upx 0upx 30upx;
	 
	}
	.login_box{
	  display:flex;
	  flex-direction:column;
	   margin:0upx auto;
	   align-items:center;
	}
	.login_box image{
	   height:140upx;
	  width:140upx;
	}
	.center_box{
	  margin-top:100upx;
	}
	/* .box_two view:nth-child(1) text{
	  color:#09BB07;
	} */
	.login_active{
	   color:#09BB07!important;
	   font-weight:bold;
	}
	/*按钮*/
	.submit-btn{
	  background-color:#09BB07;
	  margin-top:172upx;
	  width:100%;
	}
</style>
